package demo.ssm.util;

import java.util.List;

import demo.ssm.pojo.vo.DepartmentCustom;

public class TreeCodeUtil {
	private static TreeCodeUtil tree = new TreeCodeUtil(); 
	
	public static Long getCode(List<DepartmentCustom> list,int level){
		if(list==null||list.size()==0){
			int i = level-2;
			int m = 16-level-i;
			double d = Math.pow(10, m);
			Long ll=Math.round(d);
			return ll;
		}
		Long lo = 0L;
		for(DepartmentCustom de:list){
			Long l =de.getCode();
			String sl = l.toString();			
			++lo;
			if(de.getLevel()==1){
				String lsl=sl.substring(0);
				Long lsll=Long.parseLong(lsl);
					if(lo!=lsll){
						return lo*1000000000000000000L;
					}
			}else if(de.getLevel()==2){
				String lsl=sl.substring(1,3);
				Long lsll=Long.parseLong(lsl);
					if(lo!=lsll){
						return lo*10000000000000000L;
			}
			}else if(de.getLevel()==3){
				String lsl=sl.substring(3,5);
				Long lsll=Long.parseLong(lsl);
					if(lo!=lsll){
						return lo*100000000000000L;
				}
			}else if(de.getLevel()==4){
				String lsl=sl.substring(5,7);
				Long lsll=Long.parseLong(lsl);
					if(lo!=lsll){
						return lo*1000000000000L;
				}
			}else if(de.getLevel()==5){
				String lsl=sl.substring(7,9);
				Long lsll=Long.parseLong(lsl);
					if(lo!=lsll){
						return lo*10000000000L;
				}
			}else if(de.getLevel()==6){
				String lsl=sl.substring(9,11);
				Long lsll=Long.parseLong(lsl);
					if(lo!=lsll){
						return lo*100000000L;
				}
			}else if(de.getLevel()==7){
				String lsl=sl.substring(11,13);
				Long lsll=Long.parseLong(lsl);
					if(lo!=lsll){
						return lo*1000000L;
				}
			}else if(de.getLevel()==8){
				String lsl=sl.substring(13,15);
				Long lsll=Long.parseLong(lsl);
					if(lo!=lsll){
						return lo*10000L;
				}
			}else if(de.getLevel()==9){
				String lsl=sl.substring(15,17);
				Long lsll=Long.parseLong(lsl);
					if(lo!=lsll){
						return lo*100L;
				}
			}
		
		}
		int i = level-2;
		int m = 16-level-i;
		double d = Math.pow(10, m);
		Long ll=Math.round(d);
		return (list.size()+1)*ll;
	}
	public static Long getTopCode(List<DepartmentCustom> list){
		if(list==null){
			return 1000000000000000000L;
		}
		Long lo=0L;
		for(DepartmentCustom de:list){
			
			lo++;
			Long l =de.getCode();
			String sl = l.toString();
			String lsl=sl.substring(0,1);
			Long lsll=Long.parseLong(lsl);
			
				if(lo!=lsll){
						return lo*1000000000000000000L;
				}
			
		}
		double d = Math.pow(10, 18);
		Long ll=Math.round(d);
		return (list.size()+1)*ll;
	}
}
